package prassein.rsm.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.dto.DownloadChoice;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.service.base.AuditManager;
import prassein.rsm.service.base.ResumeManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name = "success",location="resumeMain.jsp"), 
	@Result(name = "error",location="login.jsp"),
	@Result(name = "input",location="resumeInsert.jsp")})
public class ResumeMainAction extends ActionSupport implements ServletContextAware {
	
	private static SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
	
	@Autowired
	private ResumeManager resumeManager;
	
	@Autowired
	private AuditManager auditManager;
	
	private static final long serialVersionUID = -7416211125815929519L;
	
	private ServletContext context;
	
	private long resumeId = -1;
	
	public int totalCount = 0;
	
	private int page;
	private int pageSize;
	
	private Resume resume = new Resume();

	private String xlsFileLocatoin;
	
	private List<Resume> resumeList;
	
	private DownloadChoice filter = new DownloadChoice();
	
	public List<Resume> getResumeList() {
		return resumeList;
	}
	public void setResumeList(List<Resume> resumeList) {
		this.resumeList = resumeList;
	}
	
	@Override 
	public String execute() throws Exception {
		initEvn();
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get(Constants.USERNAME) != null && !session.get(Constants.USERNAME).equals("")) {
			String res = SUCCESS;
			
			res = ListAllResume(session);
			
			cleanEvn();
			return res;
		} else {
			cleanEvn();
			return ERROR;
		}
	}
	
	private String ListAllResume(Map<String, Object> session) {
		List<Resume> resumeListTemp = resumeManager.getAllDesc();
		
		totalCount = resumeListTemp.size();
		
		java.util.Date date = new Date();
		resumeList = new ArrayList<Resume>();
		
		int from = page * pageSize;
		int to = from + pageSize;
		if (to > totalCount) {
			to = totalCount;
		}
		for (int i = from; i < to; i++) {
			try {
				resumeListTemp.get(i).setDateOfBirth(GetAge(date, resumeListTemp.get(i)));
			} catch (Exception e) {
				
			}
			resumeList.add(resumeListTemp.get(i));
		}
		
		session.remove(Constants.LATEST_EDIT_REUMEID);
		return SUCCESS;
	}
	
	private String GetAge(java.util.Date date, Resume item) throws ParseException {
		java.util.Date mydate = myFormatter.parse(item.getDateOfBirth());
		long day = (date.getTime()-mydate.getTime())/(24*60*60*1000) + 1;
		String year = new java.text.DecimalFormat("#").format(day/365f);
		return year + "岁";
	}
	
	private void initEvn() {
		xlsFileLocatoin = "";
	}
	private void cleanEvn() {
		resumeId = -1;
	}
	
	public ResumeManager getResumeManager() {
		return resumeManager;
	}
	public void setResumeManager(ResumeManager resumeManager) {
		this.resumeManager = resumeManager;
	}
	public long getResumeId() {
		return resumeId;
	}
	public void setResumeId(long resumeId) {
		this.resumeId = resumeId;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public Resume getResume() {
		return resume;
	}
	public void setResume(Resume resume) {
		this.resume = resume;
	}
	public String getXlsFileLocatoin() {
		return xlsFileLocatoin;
	}
	public void setXlsFileLocatoin(String xlsFileLocatoin) {
		this.xlsFileLocatoin = xlsFileLocatoin;
	}
	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
	public DownloadChoice getFilter() {
		return filter;
	}
	public void setFilter(DownloadChoice filter) {
		this.filter = filter;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public AuditManager getAuditManager() {
		return auditManager;
	}
	public void setAuditManager(AuditManager auditManager) {
		this.auditManager = auditManager;
	}
}
